//
//  movieCommentTableView.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/27.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "movieCommentTableView.h"
#import "movieCommentTableViewCell.h"
#import "common.h"
@implementation movieCommentTableView

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
//        UINib *nib = [UINib nibWithNibName:@"movieCommentTableViewCell" bundle:[NSBundle mainBundle]];
//        [self registerNib:nib forCellReuseIdentifier:@"cell"];
        
        [self registerClass:[movieCommentTableViewCell class] forCellReuseIdentifier:@"cell"];
        
    }
    return  self;
    
}

- (void)setModalArray:(NSArray *)modalArray{
    _modalArray = modalArray;
    [self reloadData];
}

#pragma mark - delegate


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _modalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    movieCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    tableModal *modal = _modalArray[indexPath.row];
    cell.modal = modal;
    if (_currentIndex == indexPath.row) {
        cell.contenSize = _contenSize;
        cell.isSpread = _isSpread;

    }
    else{
        cell.contenSize = CGSizeZero;
        cell.isSpread = NO;

    }
    return cell;
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == _currentIndex&&_isSpread == YES) {
    tableModal *modal = _modalArray[indexPath.row];
    _contenSize = [modal.content sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(Kwidth, 1000)];
        CGFloat height = _contenSize.height;
    return height+85;
    }
    else{
        return 90;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _isSlected = !_isSlected;
    _currentIndex = indexPath.row;
    if (_isSlected == NO) {
        _isSpread = NO;
        _contenSize = CGSizeZero;
        [self reloadData];
    }
    else{
    _isSpread = YES;
    [self reloadData];
    }
    
    
}
@end
